package com.wiserun.RestFul.service;

import com.alibaba.fastjson.JSON;
import com.wiserun.RestFul.mapper.PncsOrganizationMapper;
import com.wiserun.RestFul.model.Organization;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

public class Test1 {

    private List<Organization> menuList = new ArrayList<Organization>();

    public Test1(List<Organization> menuList) {
        this.menuList = menuList;
    }

    //建立树形结构
    public List<Organization> builTree() {
        List<Organization> treeMenus = new ArrayList<Organization>();//存放树形父类
        for (Organization menuNode : getRootNode()) {
            menuNode = buildChilTree(menuNode);
            System.err.println("menuNode---------------------" + menuNode.toString());
            treeMenus.add(menuNode);
        }
        System.err.println("treeMenus---------------------" + treeMenus.toString());
        return treeMenus;
    }

    private String str = "";

    //递归，建立子树形结构
    private Organization buildChilTree(Organization pNode) {
        List<Organization> chilMenus = new ArrayList<Organization>();
        for (Organization menuNode : menuList) {
            if (menuNode.getPoidOrgAdmin().equals(pNode.getOid())) {//Pnode 父对象  menunode 子对象
                System.out.println("父id================" + pNode.getOid());//父Oid
                System.out.println("子id==============" + menuNode.getOid());// 子oid
                str +=  menuNode.getOid()+"--";
                System.out.println(str);
                System.out.println("------------------------");
                chilMenus.add(buildChilTree(menuNode));
            }
        }
        pNode.setChildren(chilMenus);
        str = "";
        System.err.println("pNode---------------------" + pNode.toString());
        return pNode;
    }
    //查询当前节点的所有父级节点
    private String findAddOidIndex(Organization now){
        for (Organization parent : menuList) {
            if (now.getPoidOrgAdmin().equals(parent.getOid())) {//当前节点的poid与父级节点的oid相同，则找到了父级节点
                str += "---"+parent.getOid();
                findAddOidIndex(parent);
                return str;
            }
        }
        return  "0";//跟节点
    }
    //获取根节点
    private List<Organization> getRootNode() {
        List<Organization> rootMenuLists = new ArrayList<Organization>();
        for (Organization menuNode : menuList) {
            if (menuNode.getPoidOrgAdmin().equals("-2")) {
                rootMenuLists.add(menuNode);
            }
        }
        return rootMenuLists;
    }

    public static void main(String[] args) {
        //获取
        List<Organization> menuList = new ArrayList<Organization>();
        /*插入一些数据*/
        Organization organization=new Organization("0", "-2", "至纯科技");
        menuList.add(organization);
        Organization organization2= new Organization("1", "0", "Bu1");
        menuList.add(organization2);
        menuList.add(new Organization("1", "0", "Bu1"));
        Organization organization3 = new Organization("11", "1", "Bu11");
        menuList.add(organization3);
        menuList.add(new Organization("11", "1", "Bu11"));
        menuList.add(new Organization("12", "1", "Bu12"));
        menuList.add(new Organization("2", "0", "Bu2"));
        menuList.add(new Organization("21", "2", "Bu21"));
        menuList.add(new Organization("22", "2", "Bu22"));
        menuList.add(new Organization("3", "0", "Bu3"));
        menuList.add(new Organization("31", "3", "Bu31"));
        Organization organization4 = new Organization("32", "3", "Bu32");
        menuList.add(organization4);
        Organization organization5 = new Organization("311", "31", "Bu311");
        menuList.add(organization5);
        //menuList.add(new Organization("11", "1", "密码修改"));
/*        menuList.add(new Organization("112", "11", "新加用户"));
        menuList.add(new Organization("2", "0", "系统监控"));
        menuList.add(new Organization("22", "2", "在线用户"));
        menuList.add(new Organization("221", "22", "订阅区"));
        menuList.add(new Organization("2221", "221", "未知领域"));*/
        /*让我们创建树*/
        Test1 menuTree = new Test1(menuList);
        menuList = menuTree.builTree();

        String addOidIndex = menuTree.findAddOidIndex(organization5);
        /*转为json看看效果*/
        String jsonOutput = JSON.toJSONString(menuList);
        System.out.println(jsonOutput);
        System.out.println("_____________");
        System.err.println(addOidIndex);
    }
}
